package com.coolweather.android;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.coolweather.android.service.Code;
import com.coolweather.android.service.MD5;
import com.example.coolweather.R;


public class RegisterActivity extends AppCompatActivity {
    private Code realCode; //实际验证码
    private ImageView img_update;
    private EditText edt_username;  //用户名
    private EditText edt_password;  //密码
    private EditText edt_reconfirm; //再次确认密码
    private EditText edt_phoneCodes; //获取验证码按钮
    private ImageView iv_showCodes; //显示验证码
    private ImageView iv_back; //返回登录界面
    private Button btn_register; //注册按钮
    private String showCode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.user_register);
        super.onCreate(savedInstanceState);

        initView();//初始化界面

        initEvent();//初始化事件


        //将验证码用图片的形式显示出来

    }

    private void initView() {
        edt_username = (EditText) findViewById(R.id.edt_username);
        edt_password = (EditText) findViewById(R.id.edt_password);
        edt_reconfirm = (EditText) findViewById(R.id.edt_reconfirm);
        edt_phoneCodes = (EditText) findViewById(R.id.edt_phoneCodes);
        iv_showCodes = (ImageView) findViewById(R.id.iv_showCode);
        iv_back=(ImageView)findViewById(R.id.iv_back);
        img_update = (ImageView) findViewById(R.id.img_update);
        btn_register = (Button) findViewById(R.id.btn_regist);

    }

    private void initEvent() {
        //返回按钮
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent1);
            }
        });

        //点击输入框，查看验证码
        iv_showCodes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                realCode=Code.getInstance();
                Bitmap bitmap=realCode.createBitmap();
                iv_showCodes.setImageBitmap(bitmap);
            }
        });

        //实现注册
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框中的值
                String username = edt_username.getText().toString().trim();//获取账号
                String password = edt_password.getText().toString().trim();//获取密码
                String pwd_reconfirm=edt_reconfirm.getText().toString().trim();//获取再次输入的密码
                String phoneCode=edt_phoneCodes.getText().toString().trim();//将验证码转为String
                String codes=realCode.getCode();//获取输入的验证码

                //先判断输入框是否有值以及输入是否过长
                if(TextUtils.isEmpty(username)){
                    Toast.makeText(RegisterActivity.this,"请输入您的用户名",Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入您的密码",
                            Toast.LENGTH_SHORT).show();
                }else if(TextUtils.isEmpty(pwd_reconfirm)){
                    Toast.makeText(RegisterActivity.this,"请再次输入您的密码",Toast.LENGTH_SHORT).show();
                }else if(TextUtils.isEmpty(phoneCode)){
                    Toast.makeText(RegisterActivity.this,"请输入验证码",Toast.LENGTH_SHORT).show();
                }else if (username.length() > 20) {
                    Toast.makeText(RegisterActivity.this, "您输入的账户过长",
                            Toast.LENGTH_SHORT).show();
                }else if (password.length() > 20) {
                    Toast.makeText(RegisterActivity.this, "您输入的密码过长",
                            Toast.LENGTH_SHORT).show();
                }else if(isExistUserName(edt_username.getText().toString().trim())){
                    Toast.makeText(RegisterActivity.this,"此用户已经存在",Toast.LENGTH_SHORT).show();
                }else if(!pwd_reconfirm.equals(password)){
                    Toast.makeText(RegisterActivity.this,"两次的输入密码不一致",Toast.LENGTH_SHORT).show();
                }else if (!phoneCode.equals(codes)){
                        Toast.makeText(RegisterActivity.this, "验证码错误,注册失败", Toast.LENGTH_SHORT).show();
                } else{
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();

                    /**
                     * 保存账号和密码到SharedPreferences中
                     */
                    saveRegisterInfo(username, password);

                    //注册成功后把账号传递到LoginActivity.java中
                    // 返回值到loginActivity显示
                    Intent data=new Intent();
                    data.putExtra("userName",username);
                    setResult(RESULT_OK,data);

                    //RESULT_OK为Activity系统常量，状态码为-1，
                    // 表示此页面下的内容操作成功将data返回到上一页面，
                    // 如果是用back返回过去的则不存在用setResult传递data值
                    RegisterActivity.this.finish();
                    Intent intent2=new Intent(RegisterActivity.this,LoginActivity.class);
                    startActivity(intent2);

                }
            }
        });
    }

    /**
     * 从SharedPreferences中读取输入的用户名，判断SharedPreferences中是否有此用户名
     */
    private boolean isExistUserName(String userName) {
        boolean has_userName=false;
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        String mpassword=sp.getString(userName,"");//传入用户名获取密码
        //若mdPwd不为空则说明数据中由此用户信息，则返回true
        if(!TextUtils.isEmpty(mpassword)){
            has_userName=true;
        }
        return has_userName;
    }

    /**
     * 加密后用SharedPreferences数据类保存账户和密码
     */
    private void saveRegisterInfo(String Username, String Password) {
        String md5Psw = MD5.md5Password(Password); //把密码用MD5加密
        SharedPreferences sharedPreferences=getSharedPreferences("loginInfo", MODE_PRIVATE);

        //获取编辑器
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(Username, md5Psw);

        //提交修改 editor.commit();
        editor.apply();
    }
}